package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.dto.UploadImage;
import com.diankangwo.upyun.demo.PicBucketDemo;
import com.diankangwo.util.FileUpLoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片上传控制器
 */
@Controller
@Api(description = "图片上传")
public class FileUploadController {

    @Autowired
    private UserDAO userDao;

    /**
     * 上传到又拍云层级目录
     */
    private static String dir_root = "";

    /**
     * 上传图片
     *
     * @param files   文件
     * @param request 请求
     * @return
     */
    @ApiOperation(value = "上传图片", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile[]"),
            @ApiImplicitParam(name = "request", value = "请求", required = true, dataType = "HttpServletRequest")
    })
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile files, HttpServletRequest request) {
        FileUpLoadUtil fileUpLoadUtil = new FileUpLoadUtil();
        String url = "";
        String code = "2";
        String msg = "处理异常";
        List<UploadImage> uploadedFiles = fileUpLoadUtil.uploadFile(files, "/upload", request);
        //TODO: 存储返回图片信息到数据库
        dir_root = "/commodityPic/";
        String fileName = System.currentTimeMillis() + "";
        if (uploadedFiles != null) {
            url = PicBucketDemo.WriteFile(dir_root, new File(System.getProperty("user.dir") + "/upload/" + uploadedFiles.get(0).getRealName()), fileName);
            code = "1";
            msg = "上传成功！";
            for (UploadImage file : uploadedFiles) {
                fileUpLoadUtil.removeFile(request, file.getStoreFolder(), file.getRealName());
            }
        }
        return resultMsg.mapToJson(code, msg, url);
    }


    /**
     * 删除图片
     *
     * @param id      id
     * @param request 请求
     * @return
     */
    @ApiOperation(value = "删除图片", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "request", value = "请求", required = true, dataType = "HttpServletRequest")
    })
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Map<String, Object> delete(Integer id, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        // TODO: 根据ID从数据库获取图片信息记录
        UploadImage image = new UploadImage();

        // 删除图片
        FileUpLoadUtil fileUpLoadUtil = new FileUpLoadUtil();
        boolean deleteImage = fileUpLoadUtil.removeFile(request, image.getStoreFolder(), image.getRealName());
        if (!deleteImage) {
            // 删除失败
            // TODO：由于现在没法测试，所以这个地方写成true，后面改为注释掉的代码
//			result.put("status", deleteImage);

            result.put("status", true);
        } else {
            // 删除实际图片成功，再删除数据库记录
            // TODO: 根据ID删除图片数据库中的记录
            // 伪代码：
            // boolean deleteSuccess = service.delete(id);
            // result.put("status", deleteSuccess);
        }

        return result;
    }

}
